package com.definesys.smecrailtransit.module.SiteDetails.Fragment;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.base.SmecBaseFragment;
import com.definesys.smecrailtransit.databinding.FragmentEleParaBinding;
import com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsPresenter;
import com.definesys.smecrailtransit.module.SiteDetails.adapter.SiteDetailsParaAdapter;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleParamAchievesBean;
import com.definesys.smecrailtransit.module.SiteDetails.bean.EleParamentBean;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.hwangjr.rxbus.annotation.Subscribe;
import com.hwangjr.rxbus.annotation.Tag;
import com.hwangjr.rxbus.thread.EventThread;

import java.util.ArrayList;
import java.util.HashMap;

import static com.definesys.smecrailtransit.module.SiteDetails.SiteDetailsFragment.SiteSwitch;

/**
 * Created by cjy on 2018/3/7.
 */

public class ELeParaArchiivesFragment extends SmecBaseFragment<SiteDetailsPresenter> {
    public static final String GET_SITE_PARAM_SUCCESS="GET_SITE_PARAM_SUCCESS";
    public static final String GET_SITE_PARAM_Fail="GET_SITE_PARAM_Fail";

    private FragmentEleParaBinding fragmentEleParaBinding;
    private SiteDetailsParaAdapter siteDetailsParaAdapter;
    private static SiteDetailsPresenter siteDetailsPresenter;
    private boolean isFirst=true;
    private ArrayList<EleParamentBean> list = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        fragmentEleParaBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_ele_para,container,false);
        siteDetailsParaAdapter = new SiteDetailsParaAdapter(getContext());
        siteDetailsPresenter = new SiteDetailsPresenter(getContext());

        fragmentEleParaBinding.paramAchievesRecycle.setLayoutManager(new LinearLayoutManager(getContext()));
        fragmentEleParaBinding.paramAchievesRecycle.setAdapter(siteDetailsParaAdapter);
        _initView();
        return fragmentEleParaBinding.getRoot();
    }

    private void _initView(){
        siteDetailsParaAdapter.setList(list);
        siteDetailsParaAdapter.notifyDataSetChanged();
    }

    public static ELeParaArchiivesFragment newInstance(SiteDetailsPresenter presenter){
        Bundle args = new Bundle();
        ELeParaArchiivesFragment fragment = new ELeParaArchiivesFragment();
        siteDetailsPresenter=presenter;
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected SiteDetailsPresenter getPersenter() {
        return siteDetailsPresenter;
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_SITE_PARAM_SUCCESS)
            }
    )
    public void getSiteELeFaultSuccess(EleParamAchievesBean eleParamAchievesBean){
        if(CommonUtils.notEmpty(eleParamAchievesBean.getEleList())){
            list = eleParamAchievesBean.getEleList();
            siteDetailsParaAdapter.setList(list);
            siteDetailsParaAdapter.notifyDataSetChanged();
        }
    }

    @Subscribe(
            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(GET_SITE_PARAM_Fail)
            }
    )
    public void getSiteELeFaultFail(Object o){

    }

    @Subscribe(

            thread = EventThread.MAIN_THREAD,
            tags = {
                    @Tag(SiteSwitch)
            }
    )
    public void siteSwitch(HashMap<String,Object> hashMap){
        String ways=(String) hashMap.get("way");
        if(ways !=null && ways.equals("1")){
            if(isFirst){
                requestData(hashMap);
            }
        }else{
            requestData(hashMap);
        }
    }

    private void requestData(HashMap<String,Object> hashMap){
        int position=(Integer)hashMap.get("position");
        int stationId=hashMap.get("stationId") == null ? 0 :(Integer) hashMap.get("stationId");

        HashMap<String,Object> map = new HashMap<>();
        map.put("stationId",stationId);
        siteDetailsPresenter.getParamAchieves(map);
        isFirst=false;
    }
}
